package com.handler;

import com.entity.Gender;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/** https://mybatis.org/mybatis-3/configuration.html#typeHandlers
 *
 *
 * 继承BaseTypeHandler时指定的类型实参Gender,表示此handler可以处理的javaType为Gender
 * @author cj
 * @date 2019/10/14
 */
@MappedJdbcTypes(JdbcType.DATE)
public class GenderTypeHandler extends BaseTypeHandler<Gender> {


    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Gender parameter, JdbcType jdbcType) throws SQLException {
        System.out.println("---setNonNullParameter in GenderTypeHandler----");
        ps.setInt(i, parameter.ordinal());
    }

    @Override
    public Gender getNullableResult(ResultSet rs, String columnName) throws SQLException {
        System.out.println("---getNullableResult(rs,name) in GenderTypeHandler----");
        return rs.wasNull()?null:Gender.values()[rs.getInt(columnName)];
    }

    @Override
    public Gender getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        System.out.println("---getNullableResult(rs,index) in GenderTypeHandler----");
        return rs.wasNull()?null:Gender.values()[rs.getInt(columnIndex)];
    }

    @Override
    public Gender getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return null;
    }
}
